package com.qinggeng.project.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.common.utils.bean.BeanUtils;
import com.qinggeng.project.api.dto.GoodsClassifyDTO;
import com.qinggeng.project.system.domain.GoodsClassify;
import com.qinggeng.project.system.domain.GoodsLowerClassify;
import com.qinggeng.project.system.domain.vo.GoodsClassifyVO;
import com.qinggeng.project.system.domain.vo.GoodsLowerClassifyVO;
import com.qinggeng.project.system.mapper.GoodsClassifyMapper;
import com.qinggeng.project.system.mapper.GoodsLowerClassifyMapper;
import com.qinggeng.project.system.service.IGoodsClassifyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品分类信息Service业务层处理
 *
 * @author qinggeng
 * @date 2020-05-27
 */
@Service
public class GoodsClassifyServiceImpl implements IGoodsClassifyService {
    @Resource
    private GoodsClassifyMapper goodsClassifyMapper;

    @Resource
    private GoodsLowerClassifyMapper goodsLowerClassifyMapper;

    /**
     * 查询商品分类信息
     *
     * @param classifyId 商品分类信息ID
     * @return 商品分类信息
     */
    @Override
    public GoodsClassify selectGoodsClassifyById(Long classifyId) {
        GoodsClassify goodsClassify = goodsClassifyMapper.selectGoodsClassifyById(classifyId);
        if (null == goodsClassify) {
            throw new CustomException("找不到分类数据");
        }

        return goodsClassify;
    }

    /**
     * 查询商品分类信息列表
     *
     * @param goodsClassify 商品分类信息
     * @return 商品分类信息
     */
    @Override
    public List<GoodsClassify> selectGoodsClassifyList(GoodsClassify goodsClassify) {
//               goodsClassify.setIsSpecial(0);
        return goodsClassifyMapper.selectGoodsClassifyList(goodsClassify);
    }

    /**
     * 新增商品分类信息
     *
     * @return 结果
     */
    @Override
    public int insertGoodsClassify(GoodsClassifyDTO dto) {
        dto.setDelFalg("10");
        dto.setCreateTime(DateUtils.getNowDate());
        boolean bl=goodsClassifyMapper.insertGoodsClassify(dto)>=1;
        if (bl==false){
            throw  new CustomException("铺货失败");
        }
        return   1;
    }

    /**
     * 修改商品分类信息
     *
     * @param goodsClassify 商品分类信息
     * @return 结果
     */
    @Override
    public int updateGoodsClassify(GoodsClassify goodsClassify) {
        goodsClassify.setUpdateTime(DateUtils.getNowDate());
        int  size =goodsClassifyMapper.updateGoodsClassify(goodsClassify);
        return size;
    }

    private void deleteGoodsLowerClassify(Long classifyId) {
        Example example=new Example(GoodsLowerClassify.class);
        example.createCriteria().andEqualTo("classifyId",classifyId);
        GoodsLowerClassify goodsLower=new GoodsLowerClassify();
        goodsLower.setDelFalg(1);
        goodsLowerClassifyMapper.updateByExampleSelective(goodsLower,example);
    }

    /**
     * 批量删除商品分类信息
     *
     * @param classifyIds 需要删除的商品分类信息ID
     * @return 结果
     */
    @Override
    public int deleteGoodsClassifyByIds(Long[] classifyIds) {
        return goodsClassifyMapper.deleteGoodsClassifyByIds(classifyIds);
    }

    /**
     * 删除商品分类信息信息
     *
     * @param classifyId 商品分类信息ID
     * @return 结果
     */
    @Override
    public int deleteGoodsClassifyById(Long classifyId) {
        return goodsClassifyMapper.deleteGoodsClassifyById(classifyId);
    }

    @Override
    public int logicalDeleteByIds(Long[] classifyIds) {
        return goodsClassifyMapper.logicalDeleteByIds(classifyIds);
    }

    @Override
    public int delGoodsLowerClassifyById(Long id) {
        GoodsLowerClassify goodsLowerClassify=new GoodsLowerClassify();
        goodsLowerClassify.setId(id);
        goodsLowerClassify.setDelFalg(20);
        return goodsLowerClassifyMapper.updateByPrimaryKeySelective(goodsLowerClassify);
    }

    /**
     * 获取父分类名称等于此条件的子分类集合
     * @param classifyName
     */
    @Override
    public List<Long> selectGoodsClassifyIdsByCondition(String classifyName) {
        return goodsClassifyMapper.selectGoodsClassifyIdsByCondition(classifyName);
    }

    @Override
    public List<GoodsLowerClassifyVO> selectGoodsClassifByParntId(Long parentId) {
        List<GoodsLowerClassifyVO> goodsLowerClassifyVOS = goodsClassifyMapper.selectGoodsClassifByParentId(parentId);
        return goodsLowerClassifyVOS;
    }
}
